package AccountWindow.SystemAdmin;

import AccountWindow.SystemAdmin.AccountDetailWindow.Dao.AccountDetailDao;
import SignAndRegister.RemindWindow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class PersonalDetailWindow extends JPanel {
    public JPanel personalDetailPanel;
    private Account nowAccount;
    PersonalDetailWindow(String nowAccountID){
        //个人信息页面大致需求：1、显示个人信息；2、能够修改个人信息,只能修改用户名、用户密码和用户联系方式
        personalDetailPanel = new JPanel(null);
        personalDetailPanel.setPreferredSize(new Dimension(200,200));

        try {
            nowAccount = new AccountDetailDao().queryByID(nowAccountID);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        JLabel nowAccountIDLabel = new JLabel("您的ID：");
        JLabel nowAccountIDField = new JLabel(nowAccountID);
        JLabel nowAccountNameLabel = new JLabel("您的用户名：");
        JTextField nowAccountNameField = new JTextField(nowAccount.accountName);
        JLabel nowAccountCode = new JLabel("您的密码：");
        JTextField nowAccountCodeField = new JTextField(nowAccount.accountCode);
        JLabel nowAccountPhone = new JLabel("您的手机号码：");
        JTextField nowAccountPhoneField = new JTextField(nowAccount.accountPhone);

        personalDetailPanel.add(nowAccountIDLabel);
        personalDetailPanel.add(nowAccountIDField);
        personalDetailPanel.add(nowAccountNameLabel);
        personalDetailPanel.add(nowAccountNameField);
        personalDetailPanel.add(nowAccountCode);
        personalDetailPanel.add(nowAccountCodeField);
        personalDetailPanel.add(nowAccountPhone);
        personalDetailPanel.add(nowAccountPhoneField);

        nowAccountIDLabel.setBounds(20,20,100,20);
        nowAccountIDField.setBounds(70,20,100,20);
        nowAccountNameLabel.setBounds(20,50,100,20);
        nowAccountNameField.setBounds(90,50,110,20);
        nowAccountCode.setBounds(20,80,100,20);
        nowAccountCodeField.setBounds(80,80,120,20);
        nowAccountPhone.setBounds(20,110,100,20);
        nowAccountPhoneField.setBounds(100,110,100,20);

        JButton modifyButton = new JButton("修改");
        personalDetailPanel.add(modifyButton);
        modifyButton.setBounds(30,140,160,20);
        modifyButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver"); //注册驱动
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hims", "root", "511300");  //获取连接
                    Statement stat = con.createStatement(); //获取执行者对象
                    int row = -1;

                    String sql = "SELECT accountType FROM account WHERE accountID = '"+nowAccountID+"'";
                    ResultSet rs = stat.executeQuery(sql);

                    String type = null;
                    while(rs.next()){
                        type = rs.getString("accountType");
                    }

                    String name = nowAccountNameField.getText();
                    String code = nowAccountCodeField.getText();
                    String phone = nowAccountPhoneField.getText();
                    Account account = new Account(nowAccountID,type,code,name,phone);

                    sql = "UPDATE  account SET accountCode='"+account.accountCode+"'WHERE accountID ='"+account.accountID+"'";
                    row += stat.executeUpdate(sql);

                    if("住客".equals(account.accountType)){
                        sql = "UPDATE customer SET customerName='"+account.accountName+"',customerPhone='"+account.accountPhone+"' WHERE customerID ='"+account.accountID+"'";
                    } else if ("职工".equals(account.accountType)) {
                        sql = "UPDATE employee SET employeeName='"+account.accountName+"',employeePhone='"+account.accountPhone+"' WHERE employeeID ='"+account.accountID+"'";
                    } else if ("酒店管理员".equals(account.accountType)) {
                        sql = "UPDATE hoteladmin SET hoteladminName='"+account.accountName+"',hoteladminPhone='"+account.accountPhone+"' WHERE hoteladminID ='"+account.accountID+"'";
                    } else if ("系统管理员".equals(account.accountType)) {
                        sql = "UPDATE systemadmin SET systemadminName='"+account.accountName+"',systemadminPhone='"+account.accountPhone+"' WHERE systemadminID ='"+account.accountID+"'";
                    }

                    row += stat.executeUpdate(sql);

                    if(row == 1){
                        new RemindWindow("ModifySuccess");
                    }else{
                        new RemindWindow("ModifyFailure");
                    }

                } catch (Exception error) {
                    error.printStackTrace();
                }

            }
        });

        this.add(personalDetailPanel);

    }

}
